<!-- 考勤记工 -->
<template>
	<view>
		<bg-date @date-change="dateChange"></bg-date>
		<view class="m-3">
			<u-list @scrolltolower="scrolltolower" v-if="dataList.length > 0" style='height:calc(100vh - 150rpx)'>
				<view v-for="i, idx in dataList" :key="idx" class="bt-block block-shadow record-wrap mb-2">
					<view class="record-box" @tap="gotoDetail(i)">
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">项目名称</view>
							<view class="font-14"> {{ i.projectName }}</view>
						</view>
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">项目位置</view>
							<view class="font-14"> {{ i.projectAddr }} </view>
						</view>
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">日期</view>
							<view class="font-14"> {{ i.workDate }}</view>
						</view>
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">考勤员</view>
							<view class="font-14"> {{ i.recordUserName }}</view>
						</view>
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">工种</view>
							<view class="font-14">{{ i.workJobType }}</view>
						</view>
						<view class="flex text-line">
							<view class="font-14 text-grey2 min-w">工人</view>
							<view class="font-14">{{ i.userName }}</view>
						</view>
					</view>
					<view class="bt-line-1"></view>
					<view class="p-3">
						<view class="w-100 flex align-center">
							<view class="flex-1" style="height: 72rpx;line-height: 72rpx;">
								<!-- <text class="font-red" v-show="i.isCheckWork == 1">已确认对工</text> -->
								<text class="font-red" v-show="i.checkWorkStatus == 2">已确认对工</text>
							</view>
							<view class="flex">
								<bg-btn v-show="showCheck(i)" @click="gotoDouble(i)"
									style="width:116rpx; height:72rpx;margin-right:18rpx" :plain="true">对工</bg-btn>
								<bg-btn v-if="showEdit(i)" style="width:116rpx; height:72rpx"
									@click="gotoEdit(i)">编辑</bg-btn>
								<bg-btn v-if="!showEdit(i)" style="width:116rpx; height:72rpx"
									@click="gotoDetail(i)">详情</bg-btn>
							</view>
						</view>
					</view>
				</view>
			</u-list>
			<u-empty v-else>
			</u-empty>
		</view>
	</view>
</template>

<script>
import {
	getList
} from "@/api/attend.js"

export default {
	onNavigationBarButtonTap() {
		if (this.$auth.hasPermi("新增记工")) {
			if (!this.$store.getters.projectId) {
				uni.showToast({
					title: '暂未入场无法进行记工',
					icon: 'none',
					duration: 3000
				})
				return
			}
			uni.navigateTo({
				url: "/pages/attendance/record/record"
			})
		} else {
			this.$modal.msg("您当前暂无权限")
		}

	},
	data() {
		return {
			dataList: [],
			total: 1,
			params: {
				workDate: '',
				pageNum: 1,
				pageSize: 100000000,
			},
		}
	},
	onLoad() {
		// this.params.pageNum = 1
		// this.dataList = []
		this.getData()
		// uni.$on("refresh-page", () => {
		// 	this.params.pageNum = 1
		// 	this.dataList = []
		// 	this.getData()
		// })
	},
	onShow() {
		// this.params.pageNum = 1
		// this.dataList = []
		this.getData()
	},
	methods: {
		getData() {
			getList(this.params).then(res => {
				this.total = res.data.total
				// this.dataList = [...this.dataList, ...res.data.rows]
				this.dataList = res.data.rows
			})
		},
		gotoDetail(i) {
			uni.navigateTo({
				url: "/pages/attendance/record/detail?id=" + i.serialNumber
			})
		},
		gotoEdit(i) {
			uni.navigateTo({
				url: "/pages/attendance/record/record?id=" + i.serialNumber
			})
		},
		dateChange(e) {
			this.params.workDate = e
			this.params.pageNum = 1
			this.dataList = []
			this.getData()
		},
		scrolltolower() {
			// if (this.dataList.length < this.total) {
			// 	this.params.pageNum += 1;
			// 	this.getData()
			// }
		},
		gotoDouble(i) {
			uni.navigateTo({
				url: '/pages/attendance/record/detail?check=true&id=' + i.serialNumber
			})
		},
		// 旧逻辑
		// showCheck(i){
		// 	let date =new Date(i.workDate)
		// 	const today = new Date();
		// 	const yesterday = new Date(today);
		// 	yesterday.setDate(today.getDate() - 1);
		// 	let promise = this.$auth.hasPermi("对工")

		// 	return (
		// 		date.getDate() === yesterday.getDate() &&
		// 		date.getMonth() === yesterday.getMonth() &&
		// 		date.getFullYear() === yesterday.getFullYear()&&i.isCheckWork==0&&promise
		// 	);
		// },

		// 新-对工逻辑
		showCheck(i) {
			const workDate = new Date(i.workDate);
			workDate.setHours(0, 0, 0, 0); // 去除时间部分，确保比较准确

			const today = new Date();
			today.setHours(0, 0, 0, 0);

			const oneDayAfter = new Date(workDate);
			oneDayAfter.setDate(workDate.getDate() + 1);

			const twoDaysAfter = new Date(workDate);
			twoDaysAfter.setDate(workDate.getDate() + 2);

			const hasPermission = this.$auth.hasPermi("对工");

			// return (
			// 	today > workDate && // 今天必须在 workDate 之后
			// 	today <= twoDaysAfter && // 但不能超过 workDate + 2
			// 	i.isCheckWork == 0 &&
			// 	hasPermission === true
			// );
			// console.log('today > workDate',today > workDate);
			// console.log('today <= twoDaysAfter',today <= twoDaysAfter);
			// console.log('(i.checkWorkStatus == 0 || (this.$auth.hasRole("project") && i.checkWorkStatus == 1) || i.checkWorkStatus == 3)',(i.checkWorkStatus == 0 || (this.$auth.hasRole("project") && i.checkWorkStatus == 1) || i.checkWorkStatus == 3));
			// console.log('hasPermission === true',hasPermission === true);
			
			return (
				today > workDate && // 今天必须在 workDate 之后
				today <= twoDaysAfter && // 但不能超过 workDate + 2
				(i.checkWorkStatus == 0 || (this.$auth.hasRole("project") && i.checkWorkStatus == 1) || i.checkWorkStatus == 3) &&
				hasPermission === true
			);
		},
		showEdit(i) {
			let date = new Date(i.workDate)
			const today = new Date();
			let promise = this.$auth.hasPermi("编辑")
			return (
				date.getDate() === today.getDate() &&
				date.getMonth() === today.getMonth() &&
				date.getFullYear() === today.getFullYear() && promise
			);
		}
	}
}
</script>

<style>
.record-box {
	padding: 30rpx 24rpx;
}

.text-line {
	line-height: 56rpx;
	height: 56rpx
}

.min-w {
	min-width: 184rpx
}
</style>